﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using CRMG.Container.Views.BaseList;
using CRMG.Container.Views.InterViews;
using CRMG.Container.Views.Payments;
using CRMG.Container.Views.Training;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Windows.Forms;

namespace CRMG.Container.Views.Studnt
{
    public partial class FrmStudentList : FrmBase
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        StudentManager stdntMan;
        DataTable dtStudents;
        DataTable dtSearch;

        public FrmStudentList()
            : base()
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                this.InitializeComponent();
                this.MaximumSize = this.Size;
                this.MinimumSize = this.Size;
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmStudentList).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmStudentList).Assembly);
                stdntMan = new StudentManager();
                this.Text = formRes.GetString("FrmStudentList.Caption");
                lblSearchType.Text = formRes.GetString("FrmStudentList.lblSearchType");
                lblKey.Text = formRes.GetString("FrmStudentList.lblKey");
                lblProcessType.Text = formRes.GetString("FrmStudentList.lblProcessType");
                btnSearch.Text = formRes.GetString("FrmStudentList.btnSearch");
                btnProcess.Text = formRes.GetString("FrmStudentList.btnProcess");
                drpSearchType.SetItemSource(formRes.GetString("FrmStudentList.drpSearchType.ItemSource"), '-', '*');
                //"Adı*FirstName-Soyadı*LastName-E Posta*EMail-Tel No*Phone-Cep Tel*MobilePhone-Şirket*CompanyName"
                //"First Name*FirstName-Last Name*LastName-E Mail*EMail-Phone Number*Phone-Mobile Phone*MobilePhone-Company*CompanyName"
                drpProcessType.SetItemSource(formRes.GetString("FrmStudentList.drpProcessType.ItemSource"), '-', '*');
                // Yeni Öğr. Ekle*AddNew-Düzenle*Update-Sil*Del-Odeme Ekle*AddPay-Eğitim Ekle*AddTrain-Görüşme Ekle*AddIntrVw
                // Add New Student*AddNew-Edit Student*Update-Delete Student*Del-Add Payment*AddPay-Add Train*AddTrain-Add InterView*AddIntrVw
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2079,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2079)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentList.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
        }


        private void FormLoad(object sender, EventArgs e)
        {
            try
            {
                dtStudents = stdntMan.GetStudentList();
                dtSearch = dtStudents;
                /* Burası Load kısmına eklenecek. */
                grdVwStudentList.DataSource = dtSearch;
                //Buraya Resource dan gelen kısım eklenecek.
                grdVwStudentList.ColumnHeaderTextList = formRes.GetString("FrmStudentList.grdVwStudentList.ColumnHeaderTextList");
                grdVwStudentList.Refresh();
                //"Adı[i]-Soyadı[i]-Adı Soyadı-E Posta-Tel No-Mobil No-Şirket-OgrId[i]-Aktif[i]";
                //"FirstName[i]-LastName[i]-Full Name-E Mail-Phone Number-Mobile Phone-Company-StdntId[i]-Active[i]";
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentList.Load.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2080,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2080)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentList.Load.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void Search(object sender, EventArgs e)
        {
            try
            {
                if (drpSearchType.SelectedIndex == -1)
                {
                    ControlUtil.ShowInfoMessage(
                               messageRes.GetString("FrmStudentList.Search.IndexInvalid"),
                               messageRes.GetString("Info"));
                    return;
                }
                else
                {
                    dtSearch = null;
                    if (txtKey.Text.Length == 0)
                    {
                        dtSearch = dtStudents;
                    }
                    else
                    {
                        string strSearchType = drpSearchType.SelectionValue.ToStr();
                        dtSearch = DataUtil.FilterDataTableWithOneColContains(
                            dtStudents, strSearchType,
                            txtKey.Text.TrimStart(' ').TrimEnd(' ').ToLower());
                    }
                    grdVwStudentList.DataSource = dtSearch;
                    grdVwStudentList.Refresh();
                    txtKey.ResetText();
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentList.Search.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2081,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2081)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentList.Search.Error"),
                    messageRes.GetString("Error"));
            }
        }


        private void Process(object sender, EventArgs e)
        {
            try
            {
                if (drpProcessType.SelectedIndex == -1)
                {
                    ControlUtil.ShowInfoMessage(
                               messageRes.GetString("FrmStudentList.Process.IndexInvalid"),
                               messageRes.GetString("Info"));
                    return;
                }
                else
                {
                    if (grdVwStudentList.SelectedRows.Count < 1)
                    {
                        ControlUtil.ShowInfoMessage(
                              messageRes.GetString("FrmStudentList.Process.NoRowSelected"),
                              messageRes.GetString("Info"));
                        return;
                    }
                    else
                    {
                        string strProcess = drpProcessType.SelectionValue.ToStr();
                        int id = grdVwStudentList.SelectedRows[0].Cells["OBJID"].Value.ToInt();
                        switch (strProcess)
                        {
                            case "AddNew":
                                FrmStudent frmStnd = new FrmStudent();
                                frmStnd.ShowDialog();
                                break;
                            /*
                        case "Update":
                            FrmStudent frmStnd2 = new FrmStudent(id);
                            frmStnd2.ShowDialog();
                            break;

                        case "Del":
                            int active = grdVwStudentList.SelectedRows[0].Cells["IsActive"].Value.Obj2Int();
                            if (active == 0)
                            {
                                ControlUtil.ShowWarningMessage(
                     messageRes.GetString("FrmStudentList.Delete.AlreadyDeleted"),
                     messageRes.GetString("Warning"));
                                return;
                            }
                            else
                            {
                                System.Windows.Forms.DialogResult diaRes = ControlUtil.ShowConfirmDialog(
                                    messageRes.GetString("FrmStudentList.DeleteStudent.Confirm"),
                                    messageRes.GetString("Confirm"));
                                if (diaRes == System.Windows.Forms.DialogResult.Yes)
                                {
                                    stdntMan.DeleteStudent(id);
                                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                                    {
                                        FormName = this.Name,
                                        ControlName = btnProcess.Text,
                                        ControlTag = strProcess,
                                        LogObject = id,
                                        TransactionType = TransactionTypes.Delete
                                    };
                                    transLogger.Insert();
                                    DataUtil.UpdateDataTableWithRefColumnAndValue(dtStudents, "IsActive", 0, "OBJID", id);
                                    ControlUtil.ShowInfoMessage(
                         messageRes.GetString("FrmStudentList.Delete.Success"),
                         messageRes.GetString("Info"));
                                }
                            }
                            break;
                            */
                            case "AddPay":
                                FrmPayment frmPymnt = new FrmPayment(id);
                                frmPymnt.ShowDialog();
                                break;

                            case "AddTrain":
                                FrmStudentTrain frmStdTrain = new FrmStudentTrain(id, grdVwStudentList.SelectedRows[0].Cells["FullName"].Value.ToStr());
                                frmStdTrain.ShowDialog();
                                break;

                            case "AddIntrVw":
                                FrmInterView frmItrVw = new FrmInterView(id);
                                frmItrVw.ShowDialog();
                                break;

                            default:
                                break;
                        }
                        grdVwStudentList.DataSource = dtSearch;
                        grdVwStudentList.Refresh();
                    }
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentList.Process.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2082,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2082)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudent.Process.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void KeyChange(object sender, KeyEventArgs ke)
        {
            try
            {
                if (ke.KeyCode == Keys.F5)
                {
                    dtSearch = dtStudents;
                    grdVwStudentList.DataSource = dtSearch;
                    grdVwStudentList.Refresh();
                    txtKey.ResetText();
                    return;
                }// if (ke.KeyCode == Keys.F5) end

                if ((ke.Control & ke.Shift) == true)
                {
                    bool x = Char.IsDigit((char)ke.KeyValue) == false;
                    x &= ke.KeyValue != 16;
                    if (x == true)
                    {
                        dtSearch = DataUtil.FilterDataTableWithOneColumnValueStarts(dtStudents, "FirstName", char.ConvertFromUtf32(ke.KeyValue));
                        grdVwStudentList.DataSource = dtSearch;
                        grdVwStudentList.Refresh();
                        txtKey.ResetText();
                    }
                }// if ((ke.Control & ke.Shift) == true) end
            }
            catch (Exception ex)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2095,
                    Message = ex.Message,
                    StackTrace = ex.StackTrace,
                    Title = Rc.GetString(2095)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudent.KeyChange.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void grdVwStudentList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (grdVwStudentList.SelectedRows.Count > 0)
            {
                int stdntId = grdVwStudentList.SelectedRows[0].Cells["OBJID"].Value.ToInt();
                FrmAllStudentProcess frmAllP = new FrmAllStudentProcess(stdntId);
                frmAllP.Show();
            }
        }


    }
}
